package vip.xiaonuo.chart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.detectrecord.entity.BizDetectRecord;
import vip.xiaonuo.biz.modular.detectrecord.mapper.BizDetectRecordMapper;
import vip.xiaonuo.broiler_pro.modular.broilerproduct.mapper.BizBroilerProductMapper;
import vip.xiaonuo.chart.entity.*;
import vip.xiaonuo.chart.service.ChartService;
import vip.xiaonuo.column.modular.column.entity.BizColumn;
import vip.xiaonuo.column.modular.column.mapper.BizColumnMapper;
import vip.xiaonuo.egg_pro.modular.eggproduct.mapper.BizEggProductMapper;
import vip.xiaonuo.feed_records.modular.feedusagerecords.mapper.BizFeedUsageRecordsMapper;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author
 * @date 2023/12/20、18:08
 */
@Service
public class ChartServiceImpl implements ChartService {

    @Resource
    private BizEggProductMapper eggProductMapper;

    @Resource
    private BizBroilerProductMapper broilerProductMapper;

    @Resource
    private BizFeedUsageRecordsMapper feedUsageRecordsMapper;

    @Resource
    private BizDetectRecordMapper detectRecordMapper;

    @Resource
    private BizColumnMapper columnMapper;

    @Override
    public SickChickenTypeChart getSickChickenTypeData() {
        // 查找检测记录为异常的记录条数
        LambdaQueryWrapper<BizDetectRecord> detectRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<BizColumn> columnLambdaQueryWrapper = new LambdaQueryWrapper<>();
        detectRecordLambdaQueryWrapper.eq(BizDetectRecord::getResult,"异常");
        Long count = detectRecordMapper.selectCount(detectRecordLambdaQueryWrapper);
        Map<String,Integer> data = new HashMap<>();
        List<BizDetectRecord> bizDetectRecords = detectRecordMapper.selectList(detectRecordLambdaQueryWrapper);
        //查找对应鸡只种类的病鸡出现次数
        bizDetectRecords.forEach(record->{
            columnLambdaQueryWrapper.eq(BizColumn::getId,record.getColumnId()).select(BizColumn::getTypeId);
            BizColumn column = columnMapper.selectOne(columnLambdaQueryWrapper);
            String type = column.getTypeId();
            if(data.containsKey(type)){
                data.put(type,data.get(type) + 1);
            }else{
                data.put(type,1);
            }
            columnLambdaQueryWrapper.clear();
        });

        return new SickChickenTypeChart(count,data);
    }

    @Override
    public List<SickChickenDateNode> getSickChickenDateChart() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        LambdaQueryWrapper<BizDetectRecord> detectRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        detectRecordLambdaQueryWrapper.select(BizDetectRecord::getCreateTime,BizDetectRecord::getResult)
                .eq(BizDetectRecord::getResult,"异常").orderByAsc(BizDetectRecord::getCreateTime);
        List<BizDetectRecord> detectRecords = detectRecordMapper.selectList(detectRecordLambdaQueryWrapper);
        List<SickChickenDateNode> res = new ArrayList<>();
        Map<String,Integer> map = new LinkedHashMap<>();

        for (BizDetectRecord detectRecord : detectRecords) {
            Date date = detectRecord.getCreateTime();
            String dateStr = format.format(date);
            if (map.containsKey(dateStr)){
                map.put(dateStr,map.get(dateStr) + 1);
            }
            else{
                map.put(dateStr,1);
            }
        }

        map.forEach((key,value) ->{
            SickChickenDateNode sickChickenDateNode = new SickChickenDateNode(key, value);
            res.add(sickChickenDateNode);
        });

        return res;
    }

    @Override
    public List<SickChickenFeedNode> getSickChickenFeedData() {
        return detectRecordMapper.selectFeedCount();
    }

    @Override
    public List<SickChickenBarnNode> getSickChickenBarnData() {
        return detectRecordMapper.selectBarnCount();
    }

    @Override
    public List<EggProFeedNode> getEggproFeedData() {
        return eggProductMapper.selectEggProFeed();
    }

    @Override
    public List<BroilerProFeedNode> getBroilerProFeedData() {
        return broilerProductMapper.selectBroilerProFeed();
    }
}
